package com.xq.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Page;

/*
 CREATE TABLE `xq_comment` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `content` text NOT NULL,
 `createDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 `userId` int(11) NOT NULL,
 `parentId` int(11) DEFAULT NULL COMMENT '如果不为空表示对评论的评论。如果为空表示对目标对象（target）的评论',
 `targetId` int(11) NOT NULL COMMENT '评论目标对象id',
 `target` int(11) NOT NULL COMMENT '评论的对象类型1代表商店，2代表本地服务',
 PRIMARY KEY (`id`)
 ) ENGINE=InnoDB

 */

public class Comment extends BaseModel<Comment> {

	public final static String TABLE_NAME = "xq_comment";
	public final static Comment dao = new Comment();

	public final static int TARGET_SHOP = 1;
	public final static int TARGET_LOCAL_SERVICE = 2;

	public Page<Comment> findTopCommentsByTargetId(int target, int targetId, int pageNumber, int pageSize) {
		
		Page<Comment> pageInfo =  paginate(pageNumber, pageSize, 
				"select xq_comment.*, xq_user.id as 'user.id', "
				+ "xq_user.nick as 'user.nick', "
				+ "xq_user.portrait as 'user.portrait', "
				+ "xq_user.xiaoQuId as 'user.xiaoQuId', "
				+ "xq_user.unit as 'user.unit', "
				+ "xq_user.room as 'user.room', "
				+ "xq_xiaoqu.name as 'user.xiaoqu.name'",
				
				"from xq_comment, xq_user, xq_xiaoqu where target = ? and targetId = ? and xq_comment.parentId is null "
				+ "and xq_comment.userId =  xq_user.id and xq_user.xiaoQuId = xq_xiaoqu.id  "
				+ "order by xq_comment.createDate DESC", 
				target, targetId);
		
		/*List<Comment> commentList = pageInfo.getList();
		if (!commentList.isEmpty())
		{
			for(Comment comment: commentList)
			{
				List<Comment> childComment = findChildCommentList(comment.getInt("id"));
				comment.set("childComments", childComment);
			}
		}*/
		return pageInfo;
	}
	
	public List<Comment> findChildCommentList(int commentId)
	{
		return find("select * from xq_comment where parentId = ?", commentId);
	}
}
